package com.wswy.wzcx.ui.coupon

import android.arch.lifecycle.Observer
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.text.TextUtils
import android.view.View
import com.che.libcommon.ui.VOMessage
import com.che.libcommon.ui.loading.ILoadingView
import com.wswy.wzcx.R
import com.wswy.wzcx.model.home.RouterUtils
import com.wswy.wzcx.module.base.CBaseLoadingFragment
import kotlinx.android.synthetic.main.fragment_coupon_list.*

/**
 * 优惠券列表
 */
class CouponListFragment : CBaseLoadingFragment() {

    companion object {
        const val TAG = "mycoupon"

        @JvmStatic
        fun newInstance():CouponListFragment{
            return CouponListFragment()
        }

    }

    override fun getContentLayoutId(): Int {
        return R.layout.fragment_coupon_list
    }

    override fun getConfigure(): ILoadingView.LoadingConfigure = object : ILoadingView.SampleLoadingConfigure() {
        override fun getOnRetryClickListener(): View.OnClickListener {
            return View.OnClickListener {
                showLoading(true)
                couponViewModel.load()
            }
        }
    }

    private lateinit var couponViewModel: CouponViewModel
    private lateinit var adapter: CouponListAdapter
    private var hasCouponList = false

    private var goActFlag = false
    private var noLogin = false

    override fun getLoadingFrameId(): Int {
        return R.id.fl_content
    }

    override fun getFragmentTitle(): String {
        return "我的优惠券"
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        couponViewModel = provideViewModel(CouponViewModel::class.java)

        recycler_view.layoutManager = LinearLayoutManager(context)



        btn_ok.setOnClickListener {

            couponViewModel.getCouponRespLiveData().value?.data?.activityModel?.url?.run {
                RouterUtils.getWebIntent(context,this).apply {
                    putExtra("addToken",true)
                }.let {
                    startActivity(it)
                }

                goActFlag = true
            }

        }


        adapter = CouponListAdapter()

        recycler_view.adapter = adapter

        couponViewModel.getCouponRespLiveData().observe(this, Observer {

            it?.run {

                if (isOk()){

                    showData()

                    //有优惠活动
                    data?.activityModel?.url?.let {
                        btn_ok.visibility = View.VISIBLE
                        btn_ok.text = "我要领优惠券"
                    }?:kotlin.run {
                        btn_ok.visibility = View.GONE
                    }


                    if (data?.list.isNullOrEmpty()){
                        //没有优惠卷
                        hasCouponList = false

                        recycler_view.visibility = View.GONE

                        tv_no_coupon.visibility = View.VISIBLE

                    }else{
                        hasCouponList = true

                        tv_no_coupon.visibility = View.GONE

                        recycler_view.visibility = View.VISIBLE
                        adapter.setNewData(data?.list)
                        adapter.notifyDataSetChanged()
                    }

                }else if (inLoading()){
                    showLoading(true)
                }else if (hasError()){
                    showError(VOMessage.create(null))
                }
            }

        })

        if (couponViewModel.hasLogin()){
            couponViewModel.load()
        }else{
            activity?.run {
                noLogin = true
                couponViewModel.gotoLogin(this)
            }

        }

    }

    override fun onStart() {
        super.onStart()
        if (goActFlag){

            goActFlag = false

            if (this::couponViewModel.isInitialized){
                couponViewModel.load()
            }
        }

        if (noLogin){
            noLogin = false
            if (this::couponViewModel.isInitialized && !couponViewModel.hasLogin()){
                finish()
            }
        }

    }

}